package com.jydd.liaomingframe.welcome;

import android.Manifest;
import android.content.Intent;
import android.widget.Toast;

import com.jydd.liaomingframe.R;
import com.jydd.liaomingframe.loading.LoadingActivity;
import com.jydd.liaomingframe.login.LoginActivity;
import com.jydd.liaomingframe.main.MainActivity;
import com.lm.liaoming.base.custom.CustomBaseActivity;
import com.lm.liaoming.constant.ConstantValues;
import com.lm.liaoming.tool.Utils;
import com.tumblr.remember.Remember;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.Nullable;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * created by 韦敏敏
 * on 2019/11/18
 */
public class WelcomeActivity extends CustomBaseActivity implements EasyPermissions.PermissionCallbacks{
    private int count = 1;
    private Timer timer;
    public static final int REQUEST_ALL_PERMISSION_CODE = 10000;
    @Override
    public int setLayoutId() {
        return R.layout.activity_welcome;
    }
    @Override
    public void initVariables() {

    }
    @Override
    public void initViews() {
        requestPermissions();
    }
    /**
     * 去申请权限
     */
    @AfterPermissionGranted(REQUEST_ALL_PERMISSION_CODE)
    private void requestPermissions() {
        String[] perms = {
                Manifest.permission.CAMERA,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE
                };
        //判断有没有权限
        if (EasyPermissions.hasPermissions(this, perms)) {
            // 如果有权限了, 就做你该做的事情
            // doing something
            startIntentNext();
        } else {
            // 如果没有权限, 就去申请权限
            // this: 上下文
            // Dialog显示的正文
            // RC_CAMERA_AND_RECORD_AUDIO 请求码, 用于回调的时候判断是哪次申请
            // perms 就是你要申请的权限
            EasyPermissions.requestPermissions(this, "写上你需要用权限的理由, 是给用户看的", REQUEST_ALL_PERMISSION_CODE, perms);
        }
    }
    private void startIntentNext() {
        boolean b = Remember.getBoolean(ConstantValues.LOADING, false);
        if(b){
            if(timer != null){
                timer.cancel();
            }
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    count--;
                    Utils.i("welcome, count : " + count);
                    if(count == 0){
                        /*判断是否登录*/
                        judgeIsLogin();
                        timer.cancel();
                        timer = null;
                    }
                }
            }, 0, 1000);
        } else {
            startActivity(new Intent(this, LoadingActivity.class));
            finish();
        }
    }
    /*判断是否登录*/
    private void judgeIsLogin() {
        if(isLogined()){
            startActivity(new Intent(this, MainActivity.class));
            finish();
        } else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
    }
    /**
     * 权限申请成功的回调
     *
     * @param requestCode 申请权限时的请求码
     * @param perms       申请成功的权限集合
     */
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Utils.i("onPermissionsGranted");
        if (requestCode != REQUEST_ALL_PERMISSION_CODE) {
            return;
        }
        for (int i = 0; i < perms.size(); i++) {

        }
    }
    /**
     * 权限申请拒绝的回调
     *
     * @param requestCode 申请权限时的请求码
     * @param perms       申请拒绝的权限集合
     */
    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        Utils.i("onPermissionsDenied: ");
        if (requestCode != REQUEST_ALL_PERMISSION_CODE) {
            return;
        }
        for (int i = 0; i < perms.size(); i++) {
            if (perms.get(i).equals(Manifest.permission.CAMERA)) {
                Utils.i("onPermissionsDenied: " + "相机权限拒绝");
            } else if (perms.get(i).equals(Manifest.permission.RECORD_AUDIO)) {
                Utils.i("onPermissionsDenied: " + "录制音频权限拒绝");
            }
        }
        //如果有一些权限被永久的拒绝, 就需要转跳到 设置-->应用-->对应的App下去开启权限
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this)
                    .setTitle("权限已经被您拒绝")
                    .setRationale("如果不打开权限则无法使用该功能,点击确定去打开权限")
                    .setRequestCode(10001)//用于onActivityResult回调做其它对应相关的操作
                    .build()
                    .show();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001) {
            Toast.makeText(this, "从开启权限的页面转跳回来", Toast.LENGTH_SHORT).show();
        }
    }
}
